﻿using TreeGrid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TreeGrid
{
	public partial class TestForm : Form
	{
		public TestForm()
		{
			InitializeComponent();
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			List<TreeGridViewRow> lstRow = new List<TreeGridViewRow>();
			for (int i = 0; i < 5; i++)
			{
				TreeGridViewRow row = new TreeGridViewRow();
				row.CreateCells(treeGridView1);
				string text = string.Format("{0}", i + 1);
				row.Cells[0].Value = text;

				lstRow.Add(row);

				//treeGridView1.Rows.Add(row);
				DataGridViewRow[] rows = BuildSubRows(row);
				

				if (i % 2 == 0)
					row.IsExpand = false;
			}

			treeGridView1.Rows.AddRange(lstRow.ToArray());
		}

		private DataGridViewRow[] BuildSubRows(TreeGridViewRow row)
		{
			List<DataGridViewRow> lstRow = new List<DataGridViewRow>();
			for (int i = 0; i < 5; i++)
			{
				TreeGridViewRow subRow = new TreeGridViewRow();
				row.AddChildRow(subRow);
				subRow.CreateCells(treeGridView1);

				string text = string.Format("{0}-{1}", row.Cells[0].Value, i + 1);
				subRow.Cells[0].Value = text;

				if (i % 3 == 0)
					BuildSubSubRows(subRow);

				lstRow.Add(subRow);
			}

			return lstRow.ToArray();
		}

		private DataGridViewRow[] BuildSubSubRows(TreeGridViewRow row)
		{
			List<DataGridViewRow> lstRow = new List<DataGridViewRow>();
			for (int i = 0; i < 8; i++)
			{
				TreeGridViewRow subRow = new TreeGridViewRow();
				row.AddChildRow(subRow);
				subRow.CreateCells(treeGridView1);

				string text = string.Format("{0}-{1}", row.Cells[0].Value, i + 1);
				subRow.Cells[0].Value = text;

				if (i % 4 == 0)
					BuildSub4Rows(subRow);

				lstRow.Add(subRow);
			}

			return lstRow.ToArray();
		}

		private DataGridViewRow[] BuildSub4Rows(TreeGridViewRow row)
		{
			List<DataGridViewRow> lstRow = new List<DataGridViewRow>();
			for (int i = 0; i < 8; i++)
			{
				TreeGridViewRow subRow = new TreeGridViewRow();
				row.AddChildRow(subRow);
				subRow.CreateCells(treeGridView1);

				string text = string.Format("{0}-{1}", row.Cells[0].Value, i + 1);
				subRow.Cells[0].Value = text;

				lstRow.Add(subRow);
			}

			return lstRow.ToArray();
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		//private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		//{
		//	if (e.ColumnIndex == 0)
		//	{
		//		DataGridViewRowEx row = this.treeGridView1.Rows[e.RowIndex] as DataGridViewRowEx;
		//		if (row == null)
		//			return;

		//		if (row.ParentRow != null)
		//		{
		//			e.Value = "  " + row.Cells[0].Value;
		//		}
		//	}

		//}


		//private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		//{
		//	if (e.ColumnIndex == -1 && e.RowIndex == -1)
		//	{
		//		using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,
		//			Color.White, LinearGradientMode.ForwardDiagonal))
		//		{
		//			e.Graphics.FillRectangle(brush, e.CellBounds);
		//			Rectangle border = e.CellBounds;
		//			border.Offset(new Point(-1, -1));
		//			e.Graphics.DrawRectangle(Pens.Gray, border);
		//		}
		//		e.PaintContent(e.CellBounds);
		//		e.Handled = true;
		//	}
		//	else if (e.RowIndex == -1)
		//	{
		//		//标题行
		//		using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,
		//			Color.White, LinearGradientMode.Vertical))
		//		{
		//			e.Graphics.FillRectangle(brush, e.CellBounds);
		//			Rectangle border = e.CellBounds;
		//			border.Offset(new Point(-1, -1));
		//			e.Graphics.DrawRectangle(Pens.Gray, border);
		//		}
		//		e.PaintContent(e.CellBounds);
		//		e.Handled = true;
		//	}
		//	else if (e.ColumnIndex == -1)
		//	{
		//		//标题列
		//		using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.LightGray,
		//			Color.White, LinearGradientMode.Horizontal))
		//		{
		//			e.Graphics.FillRectangle(brush, e.CellBounds);
		//			Rectangle border = e.CellBounds;
		//			border.Offset(new Point(-1, -1));
		//			e.Graphics.DrawRectangle(Pens.Gray, border);
		//		}
		//		e.PaintContent(e.CellBounds);
		//		e.Handled = true;
		//	}
		//}

		//private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
		//{
		//	DataGridViewRowEx row = treeGridView1.Rows[e.RowIndex] as DataGridViewRowEx;
		//	if (row == null)
		//		return;

		//	Image img = null;
		//	if (row.ChildRows.Length > 0)
		//	{
		//		img = row.IsExpand ? Properties.Resources.Collapse : Properties.Resources.Expand;
		//		int top = e.RowBounds.Top + (row.Height - 15) / 2;
		//		e.Graphics.DrawImage(img, e.RowBounds.Left + this.treeGridView1.RowHeadersWidth - 20, top, 15, 15);//绘制图标
		//	}
		//}

		//private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		//{
		//	DataGridViewRowEx row = treeGridView1.Rows[e.RowIndex] as DataGridViewRowEx;
		//	if (row == null && row.ChildRows.Length <= 0)
		//		return;

		//	Rectangle bounds = new Rectangle(0, 0, treeGridView1.RowHeadersWidth, row.Height);
		//	int top = (row.Height - 15) / 2;
		//	Rectangle rc = new Rectangle(bounds.Width - 20, top, 15, 15);
		//	if (rc.Contains(e.Location))
		//	{
		//		row.IsExpand = !row.IsExpand;
		//	}
		//}
	}
}
